package com.jy.project.controller;

import com.github.pagehelper.PageInfo;
import com.jy.project.dto.ApplyDeptAdminQueryDTO;
import com.jy.project.dto.*;
import com.jy.project.service.ApplyService;
import com.jy.project.service.imp.*;
import com.jy.project.util.ServiceResultUtils;
import com.jy.project.vo.ApplyDeptAdminVO;
import com.jy.project.vo.ApplyQualitVO;
import com.jy.project.vo.ApplyUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/v1")
@CrossOrigin
@Slf4j
public class ApplyController {

    @Autowired
    @Qualifier("applyQualityServiceImpl")
    private ApplyService applyQualityServiceImpl;

    @Autowired
    @Qualifier("applyCollectServiceImpl")
    private ApplyService applyCollectServiceImpl;

    @Autowired
    @Qualifier("applyStockReturnServiceImpl")
    private ApplyService applyStockReturnServiceImpl;

    @Autowired
    @Qualifier("applyBorrowServiceImpl")
    private ApplyService applyBorrowServiceImpl;

    @Autowired
    @Qualifier("applyRenewServiceImpl")
    private  ApplyService  applyRenewServiceImpl;

    @Autowired
    @Qualifier("applyReturnServiceImpl")
    private ApplyReturnServiceImpl  applyReturnServiceImpl;

    @Autowired
    @Qualifier("applyInsertMaterialServiceImpl")
    private ApplyService applyInsertMaterialServiceImpl;

    @Autowired
    @Qualifier("applyHandoverServiceImpl")
    private ApplyService applyHandoverServiceImpl;


    @Autowired
    private  ToExamineService toExamineService;

    /**
     * 新增质量状况信息
     * */
    @RequestMapping("/apply/insertApplyQuality")
    @ResponseBody
    public Object insertApplyQuality(@RequestHeader("token") String token, ApplyQualityDTO dto){
        dto.setToken(token);
        applyQualityServiceImpl.insertApply(dto);
        return ServiceResultUtils.success(null);
    }
    /**
     * 查询质量状况列表
     * */
    @RequestMapping("/apply/listApplyQualityVO")
    @ResponseBody
    public Object listApplyQualityVO(@RequestHeader("token") String token, ApplyQueryDTO dto){
        List<ApplyQualitVO> applyQualitVOS = applyQualityServiceImpl.listApplyQualityVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(applyQualitVOS));
    }

    /**
     * 领取
     * */
    @RequestMapping("/apply/insertApplyCollect")
    @ResponseBody
    public Object insertApplyCollect(Long userId, ApplyDTO dto){
         dto.setUserId(userId);
         applyCollectServiceImpl.insertApply(dto);
        return ServiceResultUtils.success(null);
    }

    /**
     * 退库
     * */
    @RequestMapping("/apply/insertApplyStockReturn")
    @ResponseBody
    public Object insertApplyStockReturn(Long userId, @RequestBody ApplyStockReturnDTO dto){
        dto.setUserId(userId);
        applyStockReturnServiceImpl.insertApply(dto);
        return ServiceResultUtils.success(null);
    }

    /**
     * 外借
     * */
    @RequestMapping("/apply/insertApplyBorrow")
    @ResponseBody
    public Object insertApplyBorrow(@RequestHeader("token") String token, ApplyBorrowDTO dto){
        dto.setToken(token);
        applyBorrowServiceImpl.insertApply(dto);
        return ServiceResultUtils.success(null);
    }


    /**
     * 续借
     * */
    @RequestMapping("/apply/insertApplyRenew")
    @ResponseBody
    public Object insertApplyRenew(@RequestHeader("token") String token, ApplyRenewDTO dto){
        dto.setToken(token);
        applyRenewServiceImpl.insertApply(dto);
        return ServiceResultUtils.success(null);
    }

    /**
     * 归还
     * */
    @RequestMapping("/apply/insertApplyReturn")
    @ResponseBody
    public Object insertApplyReturn(@RequestHeader("token") String token, ApplyReturnDTO dto){
        dto.setToken(token);
        applyReturnServiceImpl.insertApply(dto);
        return ServiceResultUtils.success(null);
    }
    /**
     * 个人交接单
     * */
    @RequestMapping("/apply/insertApplyHandover")
    @ResponseBody
    public Object insertApplyHandover(@RequestHeader("token") String token, ApplyHandoverDTO dto){
        dto.setToken(token);
        applyHandoverServiceImpl.insertApply(dto);
        return ServiceResultUtils.success(null);
    }


    /**
     * 当前部门内的所有申请单
     * */
    @RequestMapping("/apply/dept-admin/listDeptAdminApplyVO")
    @ResponseBody
    public Object listDeptAdminApplyVO(Long userId, ApplyDeptAdminQueryDTO dto){
        dto.setApplicantUserId(userId);
        List<ApplyDeptAdminVO> applyDeptAdminVOS = applyCollectServiceImpl.listDeptAdminApplyVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(applyDeptAdminVOS));
    }

    /**
     * 审核通过
     * */
    @RequestMapping("/apply/dept-admin/adopt")
    @ResponseBody
    public Object adopt(@RequestBody ApplyAdoptDTO dto, Long userId){
        dto.setUserId(userId);
        toExamineService.applyAdopt(dto);
        return ServiceResultUtils.success(null);
    }

    /**
     * 审核驳回
     * */
    @RequestMapping("/apply/dept-admin/reject")
    @ResponseBody
    public Object reject( @RequestBody  ApplyAdoptDTO dto,Long userId){
        dto.setUserId(userId);
        toExamineService.applyReject(dto);
        return ServiceResultUtils.success(null);
    }

    /**
     *申请单详情
     * */
    @RequestMapping("/apply/listMaterialDetailsVO")
    @ResponseBody
    public Object listMaterialDetailsVO(@RequestHeader("token") String token, MaterialDetailsDTO dto){
        dto.setToken(token);
        return applyCollectServiceImpl.listMaterialDetailsVO(dto);
    }

    /**
     * 查看用户申请单
     * */
    @RequestMapping("/apply/dept-admin/listUserApplyVO")
    @ResponseBody
    public Object listUserApplyVO(Long userId, ApplyUserQueryDTO dto){
        dto.setApplicantUserId(userId);
        List<ApplyUserVO> applyUserVOS = applyInsertMaterialServiceImpl.listUserApplyVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(applyUserVOS));
    }


    /**
     * 重新发起单据
     * */
    @RequestMapping("/apply/dept-admin/reapply")
    @ResponseBody
    public Object reapply(Long userId, @RequestBody  ApplyReapplyDTO dto){
        dto.setUserId(userId);
        boolean reapply = toExamineService.reapply(dto);
        if (reapply){
            return ServiceResultUtils.success();
        }
        return ServiceResultUtils.failure();

    }


}
